package ar.edu.caece.pl.asem.codegenerator;

import java.util.HashMap;

import ar.edu.caece.pl.asem.model.impl.treeelements.AbstractElement;
import ar.edu.caece.pl.asem.model.impl.treeelements.Arreglo;
import ar.edu.caece.pl.asem.model.impl.treeelements.MetodoGenerico;
import ar.edu.caece.pl.asem.model.impl.treeelements.SimboloGenerico;

public class Assembler {

	public String start() {
		StringBuffer sb = new StringBuffer();
		sb.append("ORG 100h \n");
		sb.append("JMP main \n");
		return sb.toString();
	}

	public String generateGlobalMemory(
			HashMap<String, AbstractElement> symbolNodes) {
		StringBuffer sb = new StringBuffer();
		for (AbstractElement symbol : symbolNodes.values()) {
			if (symbol instanceof MetodoGenerico) {
				String key = "ret_null_" + symbol.getName();
				sb.append(key + " dw "
						+ "0" + " \n");
			} else {
				String key = symbol.getEnvName() + "_" + symbol.getName();
				if (symbol instanceof Arreglo) {
					int size = ((Arreglo) symbol).getLength();
					StringBuffer def = new StringBuffer();
					for (int i = 0; i < size; i++) {
						def.append(" 0,");
					}
					sb.append(key + " dw "
							+ def.toString().substring(0, def.length() - 1)
							+ " \n");
				} else {
					sb.append(key + " dw "
							+ ((SimboloGenerico) symbol).getValue() + " \n");
				}
			}
		}
		return sb.toString();
	}

}
